/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.Color;
import java.util.Hashtable;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;

import com.pearreview.application.Messages;

/**
 * This class is to use in moderator or scribe panels as bottom panel, that 
 * holds additional buttons for navigation through the forms.
 * 
 * This panel performs better usability to the top-down way 
 * of filling in the forms.
 *
 */
public class BottomButtonPanel extends JPanel {

	private static final long serialVersionUID = -3428508114187429898L;
	private JButton nextBtn;
	private JButton previousBtn;
	private JSlider fontSlider;
	private FormLabel fontLabel;
	
	public final static int FONT_MIN = 12;
	public final static int FONT_MAX = 22;
	public final static int FONT_INIT = FONT_MIN;
	
	public BottomButtonPanel() {
		initComponents();
	}
	
	private void initComponents() {
		setOpaque(false);
		setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, 
				new Color(0x828790)));
		
		nextBtn = new BottomButton();
		previousBtn = new BottomButton();
		
		initSlider();
		
		fontLabel = new FormLabel(Messages.getString("PearReview.font_size"));
		fontLabel.setIcon(new ImageIcon(
				getClass().getResource("images/zoom.png")));
		
		initNextBtn();
		initPreviousBtn();
		
		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addGap(147)
				.addComponent(previousBtn)
				.addGap(10)
				.addComponent(nextBtn)
				.addGap(10)
				.addContainerGap(0, Short.MAX_VALUE)
				.addComponent(fontLabel)
				.addGap(10)
				.addComponent(fontSlider, 
						GroupLayout.PREFERRED_SIZE, 
						GroupLayout.PREFERRED_SIZE, 
						GroupLayout.PREFERRED_SIZE)
				.addGap(10));
		
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addGap(10)
				.addGroup(layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
						.addComponent(previousBtn)
						.addComponent(nextBtn)
						.addComponent(fontLabel)
						.addComponent(fontSlider,GroupLayout.Alignment.LEADING))
				.addGap(10));
		
		setFontSliderEnabled(false);
	}
	
	private void initNextBtn() {
		nextBtn.setText(Messages.getString("PearReview.next"));
		nextBtn.setToolTipText(Messages.getString("PearReview.toolbar.next"));
		nextBtn.setOpaque(false);
		nextBtn.setIcon(new ImageIcon(
				getClass().getResource("images/go-next_22.png")));
	}
	
	private void initPreviousBtn() {
		previousBtn.setText(Messages.getString("PearReview.previous"));
		previousBtn.setToolTipText(Messages.getString(
				"PearReview.toolbar.previous"));
		previousBtn.setOpaque(false);
		previousBtn.setIcon(new ImageIcon(
				getClass().getResource("images/go-previous_22.png")));
	}
	
	/**
	 * Sets the action for changing to the next view.
	 * 
	 * @param action
	 */
	public void setNextViewAction(Action action) {
		nextBtn.setAction(action);
		initNextBtn();
	}
	
	/**
	 * Sets the action for changing to the previous view.
	 * 
	 * @param action
	 */
	public void setPreviousViewAction(Action action) {
		previousBtn.setAction(action);
		initPreviousBtn();
	}
	
	public void setFontSliderEnabled(boolean enabled) {
		fontSlider.setVisible(enabled);
		fontSlider.setEnabled(enabled);
		fontLabel.setVisible(enabled);
	}
	
	private void initSlider() {
		fontSlider = new JSlider(JSlider.HORIZONTAL,
				FONT_MIN, FONT_MAX, FONT_INIT);

		//Turn on labels at major tick marks.
		fontSlider.setMajorTickSpacing(2);
		fontSlider.setMinorTickSpacing(1);
		fontSlider.setPaintTicks(true);
		//fontSlider.setPaintLabels(true);
		
		//Create the label table
		Hashtable<Integer, JLabel> labelTable = 
			new Hashtable<Integer, JLabel>();
		for (int i=FONT_MIN; i<FONT_MAX+1; i++)
			labelTable.put(new Integer(i), new JLabel(String.valueOf(i)));
		fontSlider.setLabelTable( labelTable );
		fontSlider.setOpaque(false);
	}
	
	public void addFontSliderChangeListener(ChangeListener l) {
		fontSlider.addChangeListener(l);
	}
	
	public void removeFontSliderChangeListener(ChangeListener l) {
		fontSlider.removeChangeListener(l);
	}
	
	public int getFontSize() {
		return fontSlider.getValue();
	}
	
	public void setFontSize(int size) {
		fontSlider.setValue(size);
	}
	
	private class BottomButton extends JButton {
		private static final long serialVersionUID = -2294700063164010892L;
		@Override 
		public void setEnabled(boolean b) {
			super.setEnabled(b);
			setVisible(b);
		}
	}
}
